package com.example.poemtest.index.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.content.Context;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.example.poemtest.R;
import com.example.poemtest.index.adapter.MenuAdapter;
import com.example.poemtest.index.adapter.PoetryAdapter;
import com.example.poemtest.index.entity.StudyPoetryBean;
import com.example.poemtest.index.view.HorizontalListView;
import com.example.poemtest.utils.Constant;
import com.example.poemtest.utils.FullScreenUtil;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * 小初高页面
 */
public class StudyActivity extends AppCompatActivity {

    //展示的题目集合
    private List<Integer> showTitle;
    //题目集合
    private List<String> menuList = new ArrayList<>();
    //诗词集合
    private List<StudyPoetryBean.DataBean> poetryList = new ArrayList<>();
    //
    private HorizontalListView lv_menu;
    private ListView lv_home;

//    private TextView tv_title;

    private int currentItem;

    private MenuAdapter menuAdapter;

    private PoetryAdapter homeAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FullScreenUtil.getInstance().fullScreen(this,true);
        setContentView(R.layout.activity_study);

        initView();
        loadData();

    }
    /**
     * 加载数据
     */
    private void loadData() {
//        String json = getJson(StudyActivity.this, "category.json");
//        StudyPoetryBean poetryBean = JSONObject.parseObject(json, StudyPoetryBean.class);
        showTitle = new ArrayList<>();
        for (int i = 0; i < Constant.studyPoetries.size(); i++) {
            if (!menuList.contains(Constant.studyPoetries.get(i).getPoetryType()))
            menuList.add(Constant.studyPoetries.get(i).getPoetryType());
//            Constant.foodMap.put(dataBean.getModuleTitle(),0.0);
            showTitle.add(i);
//            poetryList.add(dataBean);
//            tv_title.setText(dataBean.getDataList().get(i).getId());
//            Log.e("分割",dataBean.getDataList().get(i).getId());
        }
//        tv_title.setText(poetryBean.getData().get(0).getDataList().get(0).getId());

        menuAdapter.notifyDataSetChanged();
        homeAdapter.notifyDataSetChanged();
    }

    private void initView() {
//        tv_title = findViewById(R.id.tv_titile);
        lv_menu = (HorizontalListView) findViewById(R.id.lv_menu);
        lv_home = (ListView) findViewById(R.id.lv_home);
//        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false);
//        lv_menu.setLayoutManger(linearLayoutManager);
        menuAdapter = new MenuAdapter(StudyActivity.this, menuList);
        lv_menu.setAdapter(menuAdapter);

        homeAdapter = new PoetryAdapter(StudyActivity.this, Constant.studyPoetries);
        lv_home.setAdapter(homeAdapter);

        lv_menu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                menuAdapter.setSelectItem(position);
                menuAdapter.notifyDataSetInvalidated();
                lv_home.setSelection(showTitle.get(position));
            }
        });


        lv_home.setOnScrollListener(new AbsListView.OnScrollListener() {
            private int scrollState;

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                this.scrollState = scrollState;
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem,
                                 int visibleItemCount, int totalItemCount) {
                if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
                    return;
                }
                int current = showTitle.indexOf(firstVisibleItem);
//				lv_home.setSelection(current);
                if (currentItem != current && current >= 0) {
                    currentItem = current;
                    menuAdapter.setSelectItem(currentItem);
                    menuAdapter.notifyDataSetInvalidated();
                }
            }
        });
    }

    /**
     * 得到json文件中的内容
     *
     * @param context
     * @param fileName
     * @return
     */
    public static String getJson(Context context, String fileName) {
        StringBuilder stringBuilder = new StringBuilder();
        //获得assets资源管理器
        AssetManager assetManager = context.getAssets();
        //使用IO流读取json文件内容
        try {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(
                    assetManager.open(fileName), "utf-8"));
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }
}